package de.neosky.filenamecopier;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.util.Arrays;
import java.util.List;

public class Tools {

	@SuppressWarnings("unchecked")
	public static String[] getFilenames(DropTargetDropEvent dtde, boolean sort, boolean fullnames) {
		String[] names = null;

		try {
			dtde.acceptDrop(DnDConstants.ACTION_COPY);

			if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
				Object obj = dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);

				if (obj instanceof List) {
					List<File> list = (List<File>) obj;
					names = new String[list.size()];

					int i = 0;
					for (File file : list) {
						names[i++] = fullnames ? file.getAbsolutePath() : file.getName();
					}

					if (sort) {
						Arrays.sort(names);
					}

				}
			}
			dtde.dropComplete(true);

		} catch (Exception ex) {
			dtde.rejectDrop();
		}

		return names;
	}

	public static void copyTextToClipboard(String str) {
		StringSelection stringSelection = new StringSelection(str);
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		clipboard.setContents(stringSelection, null);
	}

}
